﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    //业务员归还操作
    public partial class frmSampleYPJReturn : Form
    {
        Entities db = new Entities();

        T_Sample m_Sample = null;

        List<int> xID = new List<int>();

        ListViewItem lvi = null;


        //public string m_sampleProductNo
        //{
        //    set
        //    {
        //        var samplelist = db.T_Sample.Where(a => a.productNo == value);
        //        foreach (T_Sample sample in samplelist)
        //        {
        //            m_Sample = sample;
        //            break;
        //        }
        //    }
        //}
        //public string m_bowplateProductNo
        //{
        //    set
        //    {
        //        var samplelist = db.T_Sample.Where(a => a.productNo == value);
        //        foreach (T_Sample sample in samplelist)
        //        {
        //            m_Sample = sample;
        //            break;
        //        }
        //    }

        //}
        //public string m_originalshoeProductNo
        //{
        //    set
        //    {
        //        var samplelist = db.T_Sample.Where(a => a.productNo == value);
        //        foreach (T_Sample sample in samplelist)
        //        {
        //            m_Sample = sample;
        //            break;
        //        }
        //    }
        //}
        //public string m_soleProductNo
        //{
        //    set
        //    {
        //        var samplelist = db.T_Sample.Where(a => a.productNo == value);
        //        foreach (T_Sample sample in samplelist)
        //        {
        //            m_Sample = sample;
        //            break;
        //        }
        //    }
        //}
        public frmSampleYPJReturn()
        {
            InitializeComponent();
            dataGridView();
        }

        private void frmSampleYPJReturn_Load(object sender, EventArgs e)
        {
            gvShoeSample.Visible = true;
            gvBowPlate.Visible = false;
            gvOriginalShoe.Visible = false;
            gvSole.Visible = false;

            Service.Brand(cbbrand);
            Service.Cust(cbcust);
            Service.Location(cblocation);
            Service.Factory(cbfactory);

            rbShoeSample.Checked = true;

            if (Startup.GetApp().CurrentUser != null)
            {
                //tslb1.Text = "制单人：" + Startup.GetApp().CurrentUser.username;
                //tslb2.Text = "制单日期：" + DateTime.Now.ToLocalTime();
            }
        }
        private void dataGridView()
        {
            //样鞋绑定
            var gv1list = db.T_Sample.Where(a => a.srCount > 0 && a.sKind == "鞋样").ToList();
            bs1.DataSource = gv1list;

            //船头板绑定
            var gv2list = db.T_Sample.Where(a => a.srCount > 0 && a.sKind == "船头板").ToList();
            bs2.DataSource = gv2list;

            //原版鞋绑定
            var gv3list = db.T_Sample.Where(a => a.srCount > 0 && a.sKind == "原版鞋").ToList();
            bs3.DataSource = gv3list;

            //大底绑定
            var gv4list = db.T_Sample.Where(a => a.srCount > 0 && a.sKind == "大底").ToList();
            bs4.DataSource = gv4list;
        }

        private void rbGroup_CheckedChanged(object sender, EventArgs e)
        {
            if (rbShoeSample.Checked)
            {
                gvShoeSample.Visible = true;
                gvBowPlate.Visible = false;
                gvOriginalShoe.Visible = false;
                gvSole.Visible = false;
                gvShoeSample.DataSource = bs1;

                bnav.BindingSource = bs1;

            }
            else if (rbBowPlate.Checked)
            {
                gvShoeSample.Visible = false;
                gvBowPlate.Visible = true;
                gvOriginalShoe.Visible = false;
                gvSole.Visible = false;
                gvBowPlate.DataSource = bs2;

                bnav.BindingSource = bs2;

            }
            else if (rbOriginalShoe.Checked)
            {
                gvShoeSample.Visible = false;
                gvBowPlate.Visible = false;
                gvOriginalShoe.Visible = true;
                gvSole.Visible = false;
                gvOriginalShoe.DataSource = bs3;

                bnav.BindingSource = bs3;

            }
            else if (rbSole.Checked)
            {
                gvShoeSample.Visible = false;
                gvBowPlate.Visible = false;
                gvOriginalShoe.Visible = false;
                gvSole.Visible = true;
                gvSole.DataSource = bs4;

                bnav.BindingSource = bs4;

            }
        }

        private void gvShoeSample_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                lvi = new ListViewItem();

                if (bool.Parse(this.gvShoeSample.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    this.lvlist.BeginUpdate();

                    //this.m_sampleProductNo = gvShoeSample.Rows[e.RowIndex].Cells["colProductNo"].Value.ToString();


                    lvi.Text = gvShoeSample.Rows[e.RowIndex].Cells["colProductNo"].Value.ToString();
                    xID.Add(Convert.ToInt32(gvShoeSample.Rows[e.RowIndex].Cells["colxID"].Value.ToString()));
                 
                    this.lvlist.Items.Add(lvi);
                    this.lvlist.EndUpdate();

                }
                else if (bool.Parse(this.gvShoeSample.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    this.lvlist.BeginUpdate();

                    //this.m_sampleProductNo = gvShoeSample.Rows[e.RowIndex].Cells["colProductNo"].Value.ToString();

                    lvi.Text = gvShoeSample.Rows[e.RowIndex].Cells["colProductNo"].Value.ToString();

                    xID.Remove(Convert.ToInt32(gvShoeSample.Rows[e.RowIndex].Cells["colxID"].Value.ToString()));

                    foreach (ListViewItem lv in lvlist.Items)
                    {
                        if (lv.Text == lvi.Text)
                            lvlist.Items.Remove(lv);
                    }

                    this.lvlist.EndUpdate();
                }
            }
        }

        private void gvBowPlate_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                lvi = new ListViewItem();

                if (bool.Parse(this.gvBowPlate.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    this.lvlist.BeginUpdate();

                    //int index = gvShoeSample.CurrentRow.Index;    //取得选中行的索引  
                    //this.m_bowplateProductNo = gvBowPlate.Rows[e.RowIndex].Cells["colBowProductNo"].Value.ToString();


                    lvi.Text = gvBowPlate.Rows[e.RowIndex].Cells["colBowProductNo"].Value.ToString();
                    xID.Add(Convert.ToInt32(gvBowPlate.Rows[e.RowIndex].Cells["colBxID"].Value.ToString()));
                    //foreach(ListViewItem lv in lvlist.SelectedItems)
                    //{
                    //    if (lvi.Text != lv.Text)
                    //    { }     
                    //}
                    this.lvlist.Items.Add(lvi);
                    this.lvlist.EndUpdate();

                }
                else if (bool.Parse(this.gvBowPlate.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    this.lvlist.BeginUpdate();

                    //this.m_bowplateProductNo = gvBowPlate.Rows[e.RowIndex].Cells["colBowProductNo"].Value.ToString();

                    lvi.Text = gvBowPlate.Rows[e.RowIndex].Cells["colBowProductNo"].Value.ToString();
                    xID.Remove(Convert.ToInt32(gvBowPlate.Rows[e.RowIndex].Cells["colBxID"].Value.ToString()));
                    foreach (ListViewItem lv in lvlist.Items)
                    {
                        if (lv.Text == lvi.Text)
                            lvlist.Items.Remove(lv);
                    }

                    this.lvlist.EndUpdate();
                }
            }
        }

        private void gvOriginalShoe_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                lvi = new ListViewItem();

                if (bool.Parse(this.gvOriginalShoe.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    this.lvlist.BeginUpdate();

                    //int index = gvShoeSample.CurrentRow.Index;    //取得选中行的索引  
                    //this.m_originalshoeProductNo = gvOriginalShoe.Rows[e.RowIndex].Cells["colOProductNo"].Value.ToString();


                    lvi.Text = gvOriginalShoe.Rows[e.RowIndex].Cells["colOProductNo"].Value.ToString();
                    xID.Add(Convert.ToInt32(gvOriginalShoe.Rows[e.RowIndex].Cells["colOxID"].Value.ToString()));
                    //foreach(ListViewItem lv in lvlist.SelectedItems)
                    //{
                    //    if (lvi.Text != lv.Text)
                    //    { }     
                    //}
                    this.lvlist.Items.Add(lvi);
                    this.lvlist.EndUpdate();

                }
                else if (bool.Parse(this.gvOriginalShoe.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    this.lvlist.BeginUpdate();

                    //this.m_originalshoeProductNo = gvOriginalShoe.Rows[e.RowIndex].Cells["colOProductNo"].Value.ToString();

                    lvi.Text = gvOriginalShoe.Rows[e.RowIndex].Cells["colOProductNo"].Value.ToString();
                    xID.Remove(Convert.ToInt32(gvOriginalShoe.Rows[e.RowIndex].Cells["colOxID"].Value.ToString()));
                    foreach (ListViewItem lv in lvlist.Items)
                    {
                        if (lv.Text == lvi.Text)
                            lvlist.Items.Remove(lv);
                    }

                    this.lvlist.EndUpdate();
                }
            }
        }

        private void gvSole_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                lvi = new ListViewItem();

                if (bool.Parse(this.gvSole.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    this.lvlist.BeginUpdate();

                    //int index = gvShoeSample.CurrentRow.Index;    //取得选中行的索引  
                    //this.m_soleProductNo = gvSole.Rows[e.RowIndex].Cells["colSProductNo"].Value.ToString();


                    lvi.Text = gvSole.Rows[e.RowIndex].Cells["colSProductNo"].Value.ToString();
                    xID.Add(Convert.ToInt32(gvSole.Rows[e.RowIndex].Cells["colSxID"].Value.ToString()));
                    //foreach(ListViewItem lv in lvlist.SelectedItems)
                    //{
                    //    if (lvi.Text != lv.Text)
                    //    { }     
                    //}
                    this.lvlist.Items.Add(lvi);
                    this.lvlist.EndUpdate();

                }
                else if (bool.Parse(this.gvSole.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    this.lvlist.BeginUpdate();

                    //this.m_soleProductNo = gvSole.Rows[e.RowIndex].Cells["colSProductNo"].Value.ToString();

                    lvi.Text = gvSole.Rows[e.RowIndex].Cells["colSProductNo"].Value.ToString();
                    xID.Remove(Convert.ToInt32(gvSole.Rows[e.RowIndex].Cells["colSxID"].Value.ToString()));
                    foreach (ListViewItem lv in lvlist.Items)
                    {
                        if (lv.Text == lvi.Text)
                            lvlist.Items.Remove(lv);
                    }

                    this.lvlist.EndUpdate();
                }
            }
        }

        private void btnSrReturn_Click(object sender, EventArgs e)
        {
            frmSrReturnOperation frmSRO = new frmSrReturnOperation();
            frmSRO.xID = this.xID;

            if (frmSRO.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //this.dataGridView();//更新预备区数据
                this.lvlist.Items.Clear();
            }
        }

        private void btnState_Click(object sender, EventArgs e)
        {
            frmStateSelect frmSS = new frmStateSelect();
            if (rbShoeSample.Checked)
            {
                int index = gvShoeSample.CurrentRow.Index;    //取得选中行的索引  
                frmSS.m_xID = Convert.ToInt32(gvShoeSample.Rows[index].Cells["colxID"].Value.ToString());
            }
            else if (rbBowPlate.Checked)
            {
                int index = gvBowPlate.CurrentRow.Index;    //取得选中行的索引  
                frmSS.m_xID = Convert.ToInt32(gvBowPlate.Rows[index].Cells["colBxID"].Value.ToString());
            }
            else if (rbOriginalShoe.Checked)
            {
                int index = gvOriginalShoe.CurrentRow.Index;    //取得选中行的索引  
                frmSS.m_xID = Convert.ToInt32(gvOriginalShoe.Rows[index].Cells["colOxID"].Value.ToString());
            }
            else if (rbSole.Checked)
            {
                int index = gvSole.CurrentRow.Index;    //取得选中行的索引  
                frmSS.m_xID = Convert.ToInt32(gvSole.Rows[index].Cells["colOxID"].Value.ToString());
            }
            frmSS.ShowDialog();
        }

        private void btnAllChoose_Click(object sender, EventArgs e)
        {
            if (gvShoeSample.RowCount > 0)
            {
                for (int i = 0; i < gvShoeSample.RowCount; i++)
                {
                    if (bool.Parse(this.gvShoeSample.Rows[i].Cells[0].EditedFormattedValue.ToString()) == true)
                    {
                        continue;
                    }
                    else
                    {
                        this.gvShoeSample.Rows[i].Cells[0].Value = true;
                        this.lvlist.BeginUpdate();
                        lvi = new ListViewItem();

                        lvi.Text = gvShoeSample.Rows[i].Cells[2].Value.ToString();

                        xID.Add(Convert.ToInt32(gvShoeSample.Rows[i].Cells["colxID"].Value.ToString()));


                        if (!lvlist.Items.Contains(lvi))
                        {
                            lvlist.Items.Add(lvi);
                        }
                        this.lvlist.EndUpdate();
                    }
                }
            }
        }

        private void btnNoChoose_Click(object sender, EventArgs e)
        {
            if (gvShoeSample.RowCount > 0)
            {
                lvlist.Items.Clear();
                for (int i = 0; i < gvShoeSample.RowCount; i++)
                {
                    if (bool.Parse(this.gvShoeSample.Rows[i].Cells[0].EditedFormattedValue.ToString()) == true)
                    {
                        this.gvShoeSample.Rows[i].Cells[0].Value = false;
                        this.lvlist.BeginUpdate();
                        lvi = new ListViewItem();
                        lvi.Text = gvShoeSample.Rows[i].Cells[2].Value.ToString();

                        xID.Remove(Convert.ToInt32(gvShoeSample.Rows[i].Cells["colxID"].Value.ToString()));

                        lvlist.Items.Remove(lvi);

                        this.lvlist.EndUpdate();
                    }
                    else
                    {
                        this.gvShoeSample.Rows[i].Cells[0].Value = true;
                        this.lvlist.BeginUpdate();
                        lvi = new ListViewItem();

                        lvi.Text = gvShoeSample.Rows[i].Cells[2].Value.ToString();

                        xID.Add(Convert.ToInt32(gvShoeSample.Rows[i].Cells["colxID"].Value.ToString()));

                        if (!lvlist.Items.Contains(lvi))
                        {
                            lvlist.Items.Add(lvi);
                        }
                        this.lvlist.EndUpdate();
                    }
                }
            }
        }

        private void btnmemuCancel_Click(object sender, EventArgs e)
        {
            if (gvShoeSample.RowCount > 0)
            {
                for (int i = 0; i < gvShoeSample.RowCount; i++)
                {
                    if (bool.Parse(this.gvShoeSample.Rows[i].Cells[0].EditedFormattedValue.ToString()) == false)
                    {
                        continue;
                    }
                    else
                    {
                        this.gvShoeSample.Rows[i].Cells[0].Value = false;

                        xID.Remove(Convert.ToInt32(gvShoeSample.Rows[i].Cells["colxID"].Value.ToString()));

                        lvlist.Items.Clear();
                    }
                }
            }
        }

    }
}
